/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.ClieUserBean;
import com.minierp.dao.ClieUserDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.ClieUser;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class ClieUserValidator implements Validator {
	private ClieUserDao clieUserDao;

	public ClieUserDao getClieUserDao() {
		return this.clieUserDao;
	}

	public void setClieUserDao(ClieUserDao clieUserDao) {
		this.clieUserDao = clieUserDao;
	}

	public boolean supports(Class clazz) {
		return ClieUserBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		ClieUserBean bean = (ClieUserBean) obj;
		ClieUser form = bean.getUser();
		if (form.getUserName() != null && !form.getUserName().trim().equals("")) {
			if (form.getId() != 0L || bean.getCurrentPassword() != null
					&& !bean.getCurrentPassword().trim().equals("")) {
				if (form.getClieId() == 0L) {
					errors.rejectValue("user.clieId", "", "请选择『客户』");
				} else {
					boolean hasError = false;
					if (!hasError) {
						ClieUser e = this.clieUserDao.get(form.getUserName());
						if (form.getId() == 0L) {
							if (e != null) {
								hasError = true;
								errors.rejectValue("user.userName", "",
										"『登录帐号』已经存在");
							}
						} else if (e != null && e.getId() != form.getId()) {
							hasError = true;
							errors.rejectValue("user.userName", "",
									"『登录帐号』已经存在");
						}
					}

					if (!hasError
							&& (form.getId() == 0L || bean.getCurrentPassword() != null
									&& !bean.getCurrentPassword().trim()
											.equals(""))) {
						try {
							form.setPassword(EncCommonHelper.encPwd(bean
									.getCurrentPassword()));
						} catch (Exception arg6) {
							hasError = true;
							errors.rejectValue("currentPassword", "",
									"『密码』有错误：" + arg6.getMessage());
						}
					}
				}
			} else {
				errors.rejectValue("currentPassword", "", "请输入『密码』");
			}
		} else {
			errors.rejectValue("user.userName", "", "请输入『登录帐号』");
		}

	}
}